<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="关键字" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入关键字" clearable size="mini"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:help:add']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-row>
      <el-col :span="4">
        <PlatFormHelp @handleNodeClick="handleNodeClick" @handleEditClick="handleEditClick" @handleRemoveClick="handleRemoveClick"></PlatFormHelp>
      </el-col>
      <el-col :span="20">
        <el-empty v-if="(form.content === '' || form.content === null || form.content === undefined) && !open" description="暂无内容"/>
        <el-scrollbar  v-if="(form.content !== '' && form.content !== null && form.content !== undefined) && !open" >
          <div class="helpContainer" v-html="form.content"></div>
        </el-scrollbar>
        <!-- 添加或修改帮助文档对话框 -->
        <el-row v-if="open" >
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入标题" size="mini"/>
            </el-form-item>
            <el-form-item label="排序索引" prop="sort">
              <el-input v-model="form.sort" placeholder="请输入排序索引" size="mini"/>
            </el-form-item>
            <el-form-item label="状态" prop="state">
              <el-radio-group v-model="form.state" size="mini">
                <el-radio :value="0" :label="0">正常</el-radio>
                <el-radio :value="1" :label="1">停用</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="内容">
              <editor v-model="form.content" :min-height="192" size="mini"/>
            </el-form-item>
            <el-form-item>
              <el-button :loading="buttonLoading" size="mini" type="primary" @click="submitForm">确 定</el-button>
              <el-button @click="cancel" size="mini">取 消</el-button>
            </el-form-item>
          </el-form>
        </el-row>
      </el-col>
    </el-row>


  </div>
</template>

<script>
import { listHelp, getHelp, delHelp, addHelp, updateHelp, exportHelp } from "@/api/module/help";

import PlatFormHelp from "@/components/Help/index";

export default {
  name: "Help",
  components: {
    PlatFormHelp
  },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 帮助文档表格数据
      helpList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        parentId: undefined,
        sort: undefined,
        state: undefined,
        content: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询帮助文档列表 */
    getList() {
      this.loading = true;
      listHelp(this.queryParams).then(response => {
        this.helpList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleNodeClick(val){
      console.log('handleNodeClick',val);
      getHelp(val.id).then(response => {
        this.loading = false;
        this.form = response.data;
      });
    },
    handleEditClick(val){
      console.log('handleNodeClick',val);
      getHelp(val.id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
      });
    },
    handleRemoveClick(val){
      console.log('handleNodeClick',val);
      getHelp(val.id).then(response => {
        this.loading = false;
        this.form = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {

        id: undefined,
        title: undefined,
        parentId: undefined,
        sort: undefined,
        createTime: undefined,
        state: undefined,
        content: undefined      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length!==1;
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加帮助文档";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids;
      getHelp(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改帮助文档";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateHelp(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addHelp(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除帮助文档编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.loading = true;
          return delHelp(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有帮助文档数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportHelp(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>

<style lang="scss" scoped>
  .helpContainer{
    position: relative;
    padding: 20px;
  }

  ::v-deep .el-scrollbar__wrap {
    overflow: scroll;
    height: 100%;
    width: 98%;
  }

  .el-scrollbar{
    height: calc(100vh - 13rem);
  }
  ::-webkit-scrollbar {
    display: none;
  }
</style>
